package com.yundian.cloudme.action;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.print.PrintException;

import org.apache.struts2.ServletActionContext;

import com.opennice.tools.BTools;
import com.opennice.tools.Tools;
import com.opensymphony.xwork2.ActionSupport;
import com.yundian.cloudme.service.impl.CloudmeService;
import com.yundian.filebase.dao.impl.FilebaseDAO;
import com.yundian.shop.service.impl.ShopService;
import com.yundian.vo.Car;
import com.yundian.vo.Filebase;
import com.yundian.vo.Fileorder;
import com.yundian.vo.Member;

public class CloudMeAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	String result;
	Member member;
	
	/** 上传文件的属性 */
	File file;	//上传的文件
    String fileContentType;	//上传文件的类型
    String fileFileName;		//上传的文件名
    
    Integer kind;
    String fileName;
    
    CloudmeService cloudmeService;
    List listFiles;
    List listImages;
    int id;
    List listFileorders;
    Car car;
    List nearshoplist;
    List listShops;
    ShopService shopService;
    FilebaseDAO filebaseDAO;
    int successSum;
    int failSum;
    
   public String showHome(){
	    member=(Member) ServletActionContext.getRequest().getSession().getAttribute("member");
	    if(member==null)	return "login";
	    listImages= cloudmeService.browseImages(member);
	    listFiles=cloudmeService.browseFiles(member);
	    car=cloudmeService.loadCarByMember(member);
	    listFileorders=cloudmeService.browseFileState(member, car);
	    nearshoplist=shopService.browseShop();
		return "showHome";
   }
   public String showFile() throws Exception{
	   member=(Member) ServletActionContext.getRequest().getSession().getAttribute("member");
	   if(member==null)	return "login";
	    listFiles= cloudmeService.browseFiles(member);
	    car=cloudmeService.loadCarByMember(member);
	    listFileorders=cloudmeService.browseFileState(member, car);
	    nearshoplist=shopService.browseShop();
		return "showFile";
	}
   public String preSendCar(){
	   member=(Member) ServletActionContext.getRequest().getSession().getAttribute("member");
	   if(member==null)	return "login";
	    listFiles= cloudmeService.browseFiles(member);
	    car=cloudmeService.loadCarByMember(member);
	    listFileorders=cloudmeService.browseFileState(member, car);
	    nearshoplist=shopService.browseShop();
	    listShops=nearshoplist;
	    return "showShop";
   }
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   public String localPrint(){
	    member=(Member) ServletActionContext.getRequest().getSession().getAttribute("member");
	    if(member==null)	return "login";
	    car=cloudmeService.loadCarByMember(member);
	    listFileorders=cloudmeService.browseFileState(member, car);
	    ArrayList<Fileorder> fs=(ArrayList<Fileorder>) listFileorders;
	    successSum=0;
	    failSum=0;
	    for(Fileorder fileorder:fs){
	    	Filebase filebase=  filebaseDAO.getFilebase(fileorder.getOrderFileid());
	    	if(filebase!=null){
	    		File file=null;
	    		if(filebase.getFileKind()==1){
	    			file=new File(ServletActionContext.getRequest().getRealPath("/dmrc/file")+"/"+filebase.getFileUrl());
	    		}else
	    			file=new File(ServletActionContext.getRequest().getRealPath("/dmrc/image")+"/"+filebase.getFileUrl());
	    	  
	    		if(file!=null&&file.exists()){
	    			try {
						BTools.localPrint(file);
						successSum++;
					} catch (PrintException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	    		}
	    	
	    	}
	    }
	   failSum= fs.size()-successSum;
	   return "success";
   }
   
   public int getSuccessSum() {
	return successSum;
}
public void setSuccessSum(int successSum) {
	this.successSum = successSum;
}
public int getFailSum() {
	return failSum;
}
public void setFailSum(int failSum) {
	this.failSum = failSum;
}
public String showImage() throws Exception{
	   member=(Member) ServletActionContext.getRequest().getSession().getAttribute("member");
	   if(member==null)	return "login";
	   listImages= cloudmeService.browseImages(member);
	    car=cloudmeService.loadCarByMember(member);
	    listFileorders=cloudmeService.browseFileState(member, car);
	    nearshoplist=shopService.browseShop();
		return "showImage";
	}
    public List getListShops() {
	return listShops;
}
public void setListShops(List listShops) {
	this.listShops = listShops;
}
	public FilebaseDAO getFilebaseDAO() {
	return filebaseDAO;
}
public void setFilebaseDAO(FilebaseDAO filebaseDAO) {
	this.filebaseDAO = filebaseDAO;
}
	public String addFileToCar(){
    	member=(Member) ServletActionContext.getRequest().getSession().getAttribute("member");
    	car=cloudmeService.addFileToCar(member, id);
    	listFileorders=cloudmeService.browseFileState(member, car);
    	nearshoplist=shopService.browseShop();
    	return "showHome";
    }
    public String deleteOrder(){
    	member=(Member) ServletActionContext.getRequest().getSession().getAttribute("member");
    	if(id>0){
    		cloudmeService.deleteOrderFileFromCar(id);
    	}
    	car=cloudmeService.loadCarByMember(member);
	    listFileorders=cloudmeService.browseFileState(member, car);
	    nearshoplist=shopService.browseShop();
    	return "showHome";
    }
   
   
   public List getNearshoplist() {
		return nearshoplist;
	}
	public void setNearshoplist(List nearshoplist) {
		this.nearshoplist = nearshoplist;
	}
	public ShopService getShopService() {
		return shopService;
	}
	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}
public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public List getListFileorders() {
		return listFileorders;
	}
	public void setListFileorders(List listFileorders) {
		this.listFileorders = listFileorders;
	}
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
public String showWeb() throws Exception{
		return "showWeb";
	}
   public List getListImages() {
	return listImages;
}
public void setListImages(List listImages) {
	this.listImages = listImages;
}
public String shareFile(){
	   member=(Member) ServletActionContext.getRequest().getSession().getAttribute("member");
	   if(member==null)
		return "login";
	   cloudmeService.loadFile(fileName, fileName+".jpg", kind, member);
	   return "showFile";
}

public String loadFile() throws IOException{
	   System.out.println(fileFileName);
	   String url=null;
	   member=(Member) ServletActionContext.getRequest().getSession().getAttribute("member");
	   if(member==null)
		return "login";
	   if (getFileFileName()!=null && getFileFileName().trim().length()>0){
			String tempfilename = Tools.getRndFilename()+Tools.getFileExtName(getFileFileName());
			System.out.println(ServletActionContext.getRequest());
			String filename = ServletActionContext.getRequest().getRealPath("/dmrc/file").replaceAll("\\\\", "/")+"/"+tempfilename;
			System.out.println(filename);
			if(kind==2){
				filename = ServletActionContext.getRequest().getRealPath("/dmrc/image").replaceAll("\\\\", "/")+"/"+tempfilename;
			}
			FileOutputStream fos = new FileOutputStream(filename);
			FileInputStream fis = new FileInputStream(getFile());
			byte[] buf = new byte[1024];
			int len = 0;
			while((len=fis.read(buf))>0){
				fos.write(buf,0,len);
			}
			if (fis!=null)fis.close();
			if (fos!=null)fos.close();
			url=tempfilename;
		}
	   cloudmeService.loadFile(fileName, url, kind, member);
	   
	   return "showFile";
   }
   public List getListFiles() {
	return listFiles;
}
public void setListFiles(List listFiles) {
	this.listFiles = listFiles;
}
public CloudmeService getCloudmeService() {
	return cloudmeService;
}
public void setCloudmeService(CloudmeService cloudmeService) {
	this.cloudmeService = cloudmeService;
}
public File getFile() {
	return file;
}
public void setFile(File file) {
	this.file = file;
}
public String getFileContentType() {
	return fileContentType;
}
public void setFileContentType(String fileContentType) {
	this.fileContentType = fileContentType;
}
public String getFileFileName() {
	return fileFileName;
}
public void setFileFileName(String fileFileName) {
	this.fileFileName = fileFileName;
}
public String getFileName() {
	return fileName;
}
public void setFileName(String fileName) {
	this.fileName = fileName;
}
public Member getMember() {
	return member;
}
public void setMember(Member member) {
	this.member = member;
}
public String preLoadFile(){
	   member=(Member) ServletActionContext.getRequest().getSession().getAttribute("member");
	   if(member==null)
		   return "login";
	   
	   return "loadFile";
   }
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	public Integer getKind() {
		return kind;
	}
	public void setKind(Integer kind) {
		this.kind = kind;
	}
	
	
}
